package cn.charmaine.pet.controller;


import cn.charmaine.pet.domain.ProductDetail;
import cn.charmaine.pet.service.IProductDetailService;
import cn.charmaine.pet.service.IProductService;
import cn.charmaine.pet.domain.Product;
import cn.charmaine.pet.query.ProductQuery;
import cn.charmaine.basic.util.AjaxResult;
import cn.charmaine.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;
    @Autowired
    public IProductDetailService productDetailService;

    /**
     * 保存和修改公用的
     *
     * @param product 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product) {
        try {
            if (product.getId() != null) {
                productService.update(product);
            } else {
                productService.add(product);
            }
            return AjaxResult.thisSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            productService.deleteById(id);
            return AjaxResult.thisSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Product get(@PathVariable("id") Long id) {
        return productService.queryById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Product> list() {

        return productService.queryAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Product> json(@RequestBody ProductQuery query) {
        return productService.queryData(query);
    }

    /**
     * 通过pet的id查询ProductDetail
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public ProductDetail detail(@PathVariable("id") Long id){
        return productDetailService.queryByProductId(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/dels")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try{
            productService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 宠物上架审核
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids, HttpServletRequest request){
        try {
            productService.auditOnsale(ids,request);
            return AjaxResult.thisSuccess();
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.thisSuccess().setSuccess(false).setMessage("上架失败");
    }

    /**
     * 宠物下架审核
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try{
            Date date = new Date();
            System.out.println(ids);
            productService.auditOffsale(ids,date);
            return AjaxResult.thisSuccess();
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.thisSuccess().setSuccess(false).setMessage("上架失败");
    }

}
